package com.jky.trial.service.impl;

import com.jky.trial.common.*;
import com.jky.trial.constant.DataStatus;
import com.jky.trial.constant.PermissionConstant;
import com.jky.trial.elasticsearch.Projects;
import com.jky.trial.elasticsearch.ProjectsRepository;
import com.jky.trial.entity.*;
import com.jky.trial.mapper.CategoryMapper;
import com.jky.trial.mapper.ProjectMapper;
import com.jky.trial.mapper.TaskMapper;
import com.jky.trial.mapper.UserMapper;
import com.jky.trial.service.LogService;
import com.jky.trial.service.MsgService;
import com.jky.trial.service.ProjectService;
import com.jky.trial.service.RelationService;
import com.jky.trial.vo.CategoryVo;
import com.jky.trial.vo.ProjectVo;
import com.jky.trial.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class ProjectServiceImpl implements ProjectService {


    private final ProjectMapper recordMapper;
    private final UserMapper userMapper;
    private final TaskMapper taskMapper;
    private final CategoryMapper categoryMapper;
    private final MsgService msgService;
    private final LogService logService;
    private RelationService relationService;
    private ProjectsRepository projectsRepository;


    /**
     * 项目编号生成方法
     *
     * @param depNo 部门、分支机构编号
     * @param num   今年第几个项目
     * @return
     */
    public String noGenerator(String depNo, Integer num) {
//        KC-2020（ 年份）-4（哪个勘察公司）-001（今年第几个项目）？或者不带“-”
        return "KC-" + DateUtil.dateToStr(new Date(), "YYYY") + "-" + depNo + "-" +
                Constants.generateLimitNumberStr(num, 3);
    }

    @Override
    public List<ProjectVo> staSum(ProjectVo example) {

        return recordMapper.staSum(example);
    }

    @Autowired
    ProjectServiceImpl(
            TaskMapper taskMapper,
            RelationService relationService,
            CategoryMapper categoryMapper,
            LogService logService,
            ProjectsRepository projectsRepository,
            ProjectMapper recordMapper, MsgService msgService, UserMapper userMapper) {
        this.taskMapper = taskMapper;
        this.relationService = relationService;
        this.userMapper = userMapper;
        this.projectsRepository = projectsRepository;
        this.categoryMapper = categoryMapper;
        this.logService = logService; //
        this.msgService = msgService;
        this.recordMapper = recordMapper;
    }

    public long taskCount(Task record) {
        TaskExample recordExample = new TaskExample();
        TaskExample.Criteria criteria = recordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (record.getSearch() != null) { //按名称搜索
            criteria.andNameLike(record.getSearch());
            criteria.andNoLike(record.getSearch());
        }
        if (record.getProjectId() != null) {
            criteria.andProjectIdEqualTo(record.getProjectId());
        }
        if (record.getStatus() != null) { //按状态筛选
            criteria.andStatusEqualTo(record.getStatus());
        }
        return taskMapper.countByExample(recordExample);
    }

    @Override
    public int updateProgress(int id) {
        Task task = new Task();
        task.setProjectId(id);
        task.setDeleted(false);
        task.setStatus(3);//验收完成勘探点
        int finished = (int) taskCount(task);
        task.setStatus(null);//所有勘探点
        int total = (int) taskCount(task);
        Project project = new Project();
        project.setId(id);
        project.setProgress(finished + "/" + total);
        return recordMapper.updateByPrimaryKeySelective(project); //更新外业进度

    }

    @Transactional
    @Override
    public BaseResult create(Project record) {


        record.setCreateAt(new Date());
        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(record.getName());
        criteria.andDeletedEqualTo(false);//
        List<Project> list = recordMapper.selectByExample(example);
        if (list.size() != 0) {
            return new BaseResult(false, "该项目名称已经存在，不能重复添加");
        }
        ProjectExample example1 = new ProjectExample();

        ProjectExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andCreateAtBetween(
                DateUtil.strToDate("2020-01-01", DateUtil.datePattern),
                DateUtil.strToDate("2020-12-31", DateUtil.datePattern)
        );
        int count = (int) recordMapper.countByExample(example1);
        User user = userMapper.selectByPrimaryKey(Integer.valueOf(record.getCreatorId()));
        Category dep = categoryMapper.selectByPrimaryKey(user.getDepId());//获取部门
        record.setNo(this.noGenerator(
                dep.getRemark(),
                count + 1
        ));
        recordMapper.insertSelective(record);
        relationService.save(user.getId(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//项目创建人
        relationService.save(record.getTechDirector(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//技术负责人

        return new BaseResult(true, record);
    }

    @Override
    public int update(Project record) {
        Project data = recordMapper.selectByPrimaryKey(record.getId());
        if (Integer.valueOf(1).equals(data.getStatus()) &&
                Integer.valueOf(0).equals(record.getStatus())) { //归档项目
            if (Integer.valueOf(0).equals(record.getStatus())) {
                record.setArchiveAt(new Date());
            }
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria ca = categoryExample.createCriteria();
            ca.andComIdEqualTo(record.getId());
            ca.andArchiveEqualTo(false);
            List<Category> list = categoryMapper.selectByExample(categoryExample);
            if (list.size() > 0) {
                list.forEach(item -> {
                    item.setArchive(true);
                    categoryMapper.updateByPrimaryKeySelective(item);
                });
            }
        }
        if (Integer.valueOf(0).equals(data.getStatus()) &&
                Integer.valueOf(1).equals(record.getStatus())) { //取消归档项目
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria ca = categoryExample.createCriteria();
            ca.andComIdEqualTo(record.getId());
            ca.andArchiveEqualTo(true);
            List<Category> list = categoryMapper.selectByExample(categoryExample);
            if (list.size() > 0) {
                list.forEach(item -> {
                    item.setArchive(false);
                    categoryMapper.updateByPrimaryKeySelective(item);
                });
            }
        }
//        projectsRepository.
//        projectsRepository.
        return recordMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 勘察纲要审核
     * record.id required
     * record.status 操作状态：1通过，0驳回
     * record.desc 审批意见
     * record.statusOutline 审批步骤
     *
     * @param record
     * @param user
     * @return
     */
    @Override
    public BaseResult summaryCheck(Project record, User user) {
        if (record.getStatusOutline() == null) return new BaseResult(false, "参数异常");
        Project project = recordMapper.selectByPrimaryKey(record.getId());
        if (project.getDutyId() == null) return new BaseResult(false, "未设置人员信息");

        Integer action = record.getStatus(); // 审批结果
        String actionLabel = Integer.valueOf(1).equals(action) ? "通过" : "拒绝"; // 审批结果
        String desc = record.getDesc();     // 审批意见
        Integer statusOutline = record.getStatusOutline();     //
        record.setDesc(null);
        record.setStatus(null);
        //项目技术负责人
        if (statusOutline.equals(DataStatus.PROJECT_OUTLINE_STATUS_NEED_SUBMIT.getVal())) { //发起审核
            logService.log(DataStatus.LOG_DATA_TYPE_SUMMARY_CHECK.getVal(), user.getName() + "上传了"
                    + project.getName() + "项目勘察纲要", record.getId(), user.getId());
            record.setStatusOutline(DataStatus.PROJECT_OUTLINE_STATUS_NEED_DUTY.getVal());
            msgService.sendMsg(user.getId(), project.getDutyId(), "请查看" + project.getName() + "项目的勘察纲要,备注内容：" + desc, record.getId(), DataStatus.MSG_TYPE_PROJECT_SUM_TECH_UPLOAD.getVal());
        }
        //项目负责人
        if (statusOutline.equals(DataStatus.PROJECT_OUTLINE_STATUS_NEED_DUTY.getVal())) { //
            if (!user.getId().equals(project.getDutyId())) return new BaseResult(false, "您不是指定的项目负责人，不能发起审核");
            record.setStatusOutline(DataStatus.PROJECT_OUTLINE_STATUS_NEED_CHECK.getVal());//
            String logDesc = "项目负责人" + user.getName() + "发起了" + project.getName() + "项目勘察纲要的审核流程，备注内容：" + desc;
            logService.log(DataStatus.LOG_DATA_TYPE_SUMMARY_CHECK.getVal(), logDesc
                    , record.getId(), user.getId());
            msgService.sendMsg(user.getId(), project.getTechDirector(), logDesc, record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_DUTY_SUBMIT.getVal());
            msgService.sendMsg(user.getId(), project.getCheckId(), "项目负责人" + user.getName() + "发起了" + project.getName()

                            + "项目的勘察纲要审核流程,请查看" + project.getName() + "项目的勘察纲要进行审核操作", record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_DUTY_SUBMIT.getVal());
        }
        //审核人
        if (statusOutline.equals(DataStatus.PROJECT_OUTLINE_STATUS_NEED_CHECK.getVal())) { //
            if (!user.getId().equals(project.getCheckId())) return new BaseResult(false, "您不是该项目指定的审核人，不能审核");

            record.setStatusOutline(Integer.valueOf(1).equals(action) ?
                    DataStatus.PROJECT_OUTLINE_STATUS_NEED_APPROVE.getVal() : DataStatus.PROJECT_OUTLINE_STATUS_NEED_CHECK_REJECT.getVal()
            );

            String logDesc = "审核人" + user.getName() + actionLabel + "了" + project.getName() + "项目勘察纲要的审核，审核意见：" + desc;
            logService.log(DataStatus.LOG_DATA_TYPE_SUMMARY_CHECK.getVal(), logDesc, record.getId(), user.getId());
            msgService.sendMsg(user.getId(), project.getTechDirector(), logDesc, record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_CHECK_SUBMIT.getVal());
            msgService.sendMsg(user.getId(), project.getDutyId(), logDesc, record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_CHECK_SUBMIT.getVal());
            if (Integer.valueOf(1).equals(action)) { //通过
                msgService.sendMsg(user.getId(), project.getApproveId(), "审核人" + user.getName() + "通过了" + project.getName() + "项目的审核，请查看" + project.getName() + "项目的勘察纲要进行审定", record.getId(),
                        DataStatus.MSG_TYPE_PROJECT_SUM_CHECK_SUBMIT.getVal());
            }

        }
        //审定人
        if (statusOutline.equals(DataStatus.PROJECT_OUTLINE_STATUS_NEED_APPROVE.getVal())) { //
            if (!user.getId().equals(project.getApproveId())) return new BaseResult(false, "您不是该项目指定的审定人，不能审定");
            record.setStatusOutline(Integer.valueOf(1).equals(action) ? DataStatus.PROJECT_OUTLINE_STATUS_NEED_APPROVE_RESOLVE.getVal() : DataStatus.PROJECT_OUTLINE_STATUS_NEED_APPROVE_REJECT.getVal());
            String logDesc = "审定人" + user.getName() + actionLabel + "了" + project.getName() + "项目勘察纲要的审批，审批意见：" + desc;
            logService.log(DataStatus.LOG_DATA_TYPE_SUMMARY_CHECK.getVal(), logDesc, record.getId(), user.getId());
            msgService.sendMsg(user.getId(), project.getTechDirector(), logDesc, record.getId(), DataStatus.
                    MSG_TYPE_PROJECT_SUM_APPROVE_SUBMIT.getVal());
            msgService.sendMsg(user.getId(), project.getDutyId(), logDesc, record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_APPROVE_SUBMIT.getVal());
            msgService.sendMsg(user.getId(), project.getCheckId(), logDesc, record.getId(),
                    DataStatus.MSG_TYPE_PROJECT_SUM_APPROVE_SUBMIT.getVal());

        }
        return new BaseResult(true, recordMapper.updateByPrimaryKeySelective(record));
    }

    @Override
    public BaseResult staticProject(ProjectVo record1, User user) {

        Subject subject = SecurityUtils.getSubject();//
        Project record = new Project();
        Map map = new HashMap<String, Integer>();
        record.setDeleted(false);
        record.setStatus(null);
        if (
                subject.isPermitted(PermissionConstant.BUS_PROJECT) ||
                        subject.isPermitted(PermissionConstant.COM_PROJECT) ||
                        subject.isPermitted(PermissionConstant.DEP_PROJECT)) {
            if (subject.isPermitted(PermissionConstant.COM_PROJECT)) {//被授予公司或部门项目权限

                map.put("total", this.count(record));
                record.setStatus(DataStatus.PROJECT_ARCHIVE.getVal());//已归档
                map.put("archive", this.count(record));
                record.setStatus(DataStatus.PROJECT_PROGRESSING.getVal());//勘察中
                map.put("progressing", this.count(record));
                record.setStatus(DataStatus.PROJECT_END.getVal());//已终止
                map.put("end", this.count(record));
                return new BaseResult(true, map);
            } else if (subject.isPermitted(PermissionConstant.BUS_PROJECT)) {
                record1.setBusId(user.getBusId());
            } else if (subject.isPermitted(PermissionConstant.DEP_PROJECT)) {//record.setBusId(user.getBusId());
                record1.setDepId(user.getDepId());
            }
        } else {
            record1.setOwnerId(user.getId());
        }
        record1.setDeleted(false);
        record1.setStatus(null);
        map.put("total", recordMapper.selfCountWithName(record1));
        record1.setStatus(DataStatus.PROJECT_ARCHIVE.getVal());//已归档
        map.put("archive", recordMapper.selfCountWithName(record1));
        record1.setStatus(DataStatus.PROJECT_PROGRESSING.getVal());//勘察中
        map.put("progressing", recordMapper.selfCountWithName(record1));
        record1.setStatus(DataStatus.PROJECT_END.getVal());//已终止
        map.put("end", recordMapper.selfCountWithName(record1));
        return new BaseResult(true, map);

    }

    @Override
    public int setPlan(Project record, User user) {
        ;
        return recordMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 人员设置
     *
     * @param record
     * @return
     */
    @Transactional
    @Override
    public int setPerson(Project record, User user) {
        Project project = recordMapper.selectByPrimaryKey(record.getId());
        if (record.getTechDirector() != null && !record.getTechDirector().equals(project.getTechDirector())) {
            User techor = userMapper.selectByPrimaryKey(record.getTechDirector());
            relationService.save(techor.getId(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//技术负责人

            msgService.sendMsg(
                    user.getId(),
                    record.getTechDirector(),
                    techor.getName() + "同事,您被指定为" + project.getName() + "项目的技术负责人,请及时跟进项目",
                    record.getId(),     //
                    DataStatus.MSG_TYPE_PROJECT_SET_PERSON.getVal()
            );
        }
        if (record.getDutyId() != null && !record.getDutyId().equals(project.getDutyId())) {
            User person = userMapper.selectByPrimaryKey(record.getDutyId());
            relationService.save(person.getId(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//项目负责人

            msgService.sendMsg(
                    user.getId(),
                    record.getDutyId(),
                    person.getName() + "同事,您被指定为" + project.getName() + "项目的项目负责人,请及时跟进项目",
                    record.getId(),     //
                    DataStatus.MSG_TYPE_PROJECT_SET_PERSON.getVal()
            );
        }
        if (record.getCheckId() != null && !record.getCheckId().equals(project.getCheckId())) {
            User person = userMapper.selectByPrimaryKey(record.getCheckId());
            relationService.save(person.getId(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//勘察纲要审核人

            msgService.sendMsg(
                    user.getId(),
                    record.getCheckId(),
                    person.getName() + "同事,您被指定为" + project.getName() + "项目的勘察纲要审核人,请及时跟进项目",
                    record.getId(),     //
                    DataStatus.MSG_TYPE_PROJECT_SET_PERSON.getVal()
            );
        }
        if (record.getApproveId() != null && !record.getApproveId().equals(project.getApproveId())) {
            User person = userMapper.selectByPrimaryKey(record.getApproveId());
            relationService.save(person.getId(), record.getId(), DataStatus.RELATION_TYPE_USER_PROJECT.getVal());//勘察纲要审定人
            msgService.sendMsg(
                    user.getId(),
                    record.getApproveId(),
                    person.getName() + "同事,您被指定为" + project.getName() + "项目的勘察纲要审定人,请及时跟进项目",
                    record.getId(),     //
                    DataStatus.MSG_TYPE_PROJECT_SET_PERSON.getVal()
            );
        }
        return recordMapper.updateByPrimaryKeySelective(record);//
    }


    @Transactional
    @Override
    public int del(int id) {
        Project project = new Project();
        project.setId(id);
        project.setDeleted(true);
        msgService.delBy(id);//删除实体
        Projects projects = new Projects();
        projects.setTarget_id(id);
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("target_id", id);
        Iterable iterable = projectsRepository.search(queryBuilder);
        projectsRepository.deleteAll(iterable);
        return recordMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public Project detail(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    public ProjectVo detailVo(int id) {
        ProjectVo detail = (ProjectVo) recordMapper.selectByPrimaryKey(id);
        User techDirector = userMapper.selectByPrimaryKey(detail.getTechDirector());
        detail.setTechDirectorName(techDirector.getName());
        return null;
    }

    /**
     * 包含创建人、技术负责人、勘察单位、委托单位、施工单位人员姓名的详情查询
     *
     * @param id
     * @return
     */
    @Override
    public ProjectVo moreDetail(int id) {
        ProjectVo record = new ProjectVo();
        record.setId(id);
        List<ProjectVo> list = recordMapper.pageListWithName(record);
        ProjectVo projectVo = list.get(0);
        List<User> joiners = new ArrayList(); //参与用户
        if (projectVo.getDutyId() != null) {
            User duty = userMapper.selectByPrimaryKey(projectVo.getDutyId());
            projectVo.setDutyName(duty.getName());
            joiners.add(duty);
        }
        ;
        if (projectVo.getCheckId() != null) {
            User checker = userMapper.selectByPrimaryKey(projectVo.getCheckId());
            projectVo.setCheckName(checker.getName());
            joiners.add(checker);
        }
        ;
        if (projectVo.getApproveId() != null) {
            User approve = userMapper.selectByPrimaryKey(projectVo.getApproveId());
            projectVo.setApproveName(approve.getName());
            joiners.add(approve);
        }
        ;
        projectVo.setJoiners(joiners);//

        List<CategoryVo> coms = new ArrayList(); //参与单位

        if (projectVo.getCom2() != null) {
            CategoryVo categoryVo = categoryMapper.selectOneByKey(projectVo.getCom2());
            if (categoryVo.getPersonId() != null) { //业务单位联系人
                Category person = categoryMapper.selectByPrimaryKey(categoryVo.getPersonId());
                categoryVo.setUser(person);
            }
            coms.add(categoryVo);
        }
        if (projectVo.getCom3() != null) {
            CategoryVo categoryVo = categoryMapper.selectOneByKey(projectVo.getCom3());
            if (categoryVo.getPersonId() != null) { //业务单位联系人
                Category person = categoryMapper.selectByPrimaryKey(categoryVo.getPersonId());
                categoryVo.setUser(person);
            }
            coms.add(categoryVo);
        }
        if (projectVo.getCom1() != null) {
            CategoryVo categoryVo = categoryMapper.selectOneByKey(projectVo.getCom1());
            if (categoryVo.getPersonId() != null) { //业务单位联系人
                Category person = categoryMapper.selectByPrimaryKey(categoryVo.getPersonId());
                categoryVo.setUser(person);
            }
            coms.add(categoryVo);
        }
        projectVo.setComs(coms);
        return projectVo;
    }

    @Override
    public List<ProjectVo> list(ProjectVo record) {
        record.setDeleted(false);
        return recordMapper.pageList(record);
    }

    @Override
    public BaseResult personSta(ProjectVo record) {
        Map map = new HashMap();
        map.put("projectDuty", recordMapper.staPersonProject(record));
        map.put("projectDetail", recordMapper.staPersonProjectDetail(record));
        return new BaseResult(true, map);
    }

    @Override
    public BaseResult depSta(ProjectVo record) {
        Map map = new HashMap();
        UserVo user = new UserVo();
        user.setSize(Integer.MAX_VALUE);
        user.setDepId(record.getDepId());
        List<UserVo> list = userMapper.pageList(user);
        list.forEach(item -> {
            record.setId(item.getId());
            item.setListProject(recordMapper.staPersonProject(record));
            item.setProject(recordMapper.staPersonProjectStatus(record));
            item.setPassword(null);
        });
        map.put("members", list);//部门成员数据统计
        return new BaseResult(true, map);
    }

    @Override
    public BaseResult depStaMonth(ProjectVo record) {
        Map map = new HashMap();
        record.setDesc("create_at");
        record.setStatus(DataStatus.PROJECT_PROGRESSING.getVal());
        map.put("new", recordMapper.staDepProjectByMonth(record));//部门成员数据统计
        record.setDesc(null);
        record.setStatus(DataStatus.PROJECT_ARCHIVE.getVal());
        map.put("archive", recordMapper.staDepProjectByMonth(record));//部门成员数据统计
        return new BaseResult(true, map);
    }

    @Override
    public BaseResult busMonthSta(ProjectVo record) {
        return depStaMonth(record);
    }

    @Override
    public BaseResult comMonthSta(ProjectVo record) {
        record.setBusId(null);
        record.setDepId(null);
        return depStaMonth(record);
    }

    @Override
    public BaseResult busSta(ProjectVo record) {
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setType(DataStatus.CATEGORY_DATA_TYPE_DEP.getVal());
        categoryVo.setDeleted(false);
        categoryVo.setpId(record.getBusId());
        categoryVo.setSize(Integer.MAX_VALUE);
        List<CategoryVo> deps = categoryMapper.pageList(categoryVo);
        Map map = new HashMap();
        deps.forEach(item -> {
            record.setDepId(item.getId());
            item.setProject(recordMapper.staDepProjectStatus(record));
        });
        map.put("deps", deps);
        return new BaseResult(true, map);
    }


    @Override
    public BaseResult comSta(ProjectVo record) {
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setType(DataStatus.CATEGORY_DATA_TYPE_BUS.getVal());
        categoryVo.setDeleted(false);
        categoryVo.setpId(record.getBusId());
        categoryVo.setSize(Integer.MAX_VALUE);
        List<CategoryVo> bus = categoryMapper.pageList(categoryVo);
        Map map = new HashMap();
        bus.forEach(item -> {
            record.setBusId(item.getId());
            item.setProject(recordMapper.staDepProjectStatus(record));
        });
        map.put("bus", bus);
        return new BaseResult(true, map);
    }

    /**
     * 项目明细列表
     *
     * @param record
     * @return
     */
    @Override
    public ListResult projectDetail(ProjectVo record) {
        return new ListResult(true, recordMapper.staPersonProjectDetail(record));
    }

    @Override
    public ListResult page(ProjectVo record) {
        List<ProjectVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                recordMapper.pageCount(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult pageWithName(ProjectVo record) {
        record.setDeleted(false);
        List<ProjectVo> list = recordMapper.pageListWithName(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult outlinePageWithName(ProjectVo record) {
        record.setDeleted(false);
        List<ProjectVo> list = recordMapper.selfOutlinePageListWithName(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult selfOutlinePageWithName(ProjectVo record) {
        record.setDeleted(false);
        List<ProjectVo> list = recordMapper.selfOutlinePageListWithName(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult descPage(ProjectVo record) {
        record.setDeleted(false);
        record.setStatus(1);
//        record.setStatusOutline(41);
        List<ProjectVo> list = recordMapper.selectDescByPage(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) recordMapper.countDescByPage(record),
                record.getSize()
        ));
        return res;
    }

    /**
     * record.ownerId   个人层面
     * record.depId     部门层面
     * record.busId     业务板块层面
     *
     * @param record
     * @return
     */
    @Override
    public ListResult selfPageWithName(ProjectVo record) {
        record.setDeleted(false);
        List<ProjectVo> list = recordMapper.selfPageListWithName(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                recordMapper.selfCountWithName(record),
                record.getSize()
        ));
        return res;
    }


    @Override
    public long count(Project record) {
        ProjectExample recordExample = new ProjectExample();
        ProjectExample.Criteria criteria = recordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (record.getSearch() != null) { //按名称搜索
            criteria.andNameLike(record.getSearch());
            criteria.andNoLike(record.getSearch());
        }
        if (record.getStatus() != null) { //按状态筛选
            criteria.andStatusEqualTo(record.getStatus());
        }
        if (record.getStatusOutline() != null) { //
            criteria.andStatusOutlineEqualTo(record.getStatusOutline());
        }
        return recordMapper.countByExample(recordExample);
    }


}
